<template>
	<view class="container">
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-forms-item label="标签编码" class="tagCode">
				<uni-easyinput disabled suffixIcon="scan" @iconClick="scan" type="text" v-model="formData.tagCode"
					placeholder="扫码或手动输入标签编码" />
			</uni-forms-item>
			<uni-forms-item label="标签名称" class="tagName">
				<uni-easyinput type="text" v-model="formData.tagName" placeholder="请输入标签名称" />
			</uni-forms-item>
		</uni-forms>
		<button @click="submit()" size="small" style="width: 100%;" type="primary">保存</button>
	</view>
</template>

<script>
	import request from "@/common/request.js";
	export default {
		onLoad: function(option) {
			// #ifdef APP-NVUE
			const eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
			// #endif
			// #ifndef APP-NVUE
			const eventChannel = this.getOpenerEventChannel();
			// #endif
			// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			eventChannel.on('acceptDataFromOpenerPage', (data) => {
				this.formData.tagCode = data.tagCode
				this.formData.tagName = data.tagName
			})

			//回复事件及内容：
			// eventChannel.emit('acceptDataFromOpenedPage', {data: 'test'});
			// eventChannel.emit('someEvent', {data: 'test'});
		},
		data() {
			return {
				// 表单数据
				formData: {
					tagName: '',
					tagCode: ''
				},
				rules: {
					// 对tagCode字段进行必填验证
					tagCode: {
						rules: [{
							required: true,
							errorMessage: '扫码或手动输入标签编码',
						}, ]
					},
					tagName: {
						rules: [{
							required: true,
							errorMessage: '请输入标签名称',
						}, ]
					},
				},
				label_data: [{
					labelName: '',
					deviceNo: '',
					groupID: '',
					longitude: '',
					remark: '',
				}]
			}
		},
		methods: {
			// 触发提交表单
			submit() {
				this.$refs.form.validate().then(res => {
					co6nsole.log('表单数据信息：', res);
				}).catch(err => {
					console.log('表单错误信息：', err);
				})

				let opts = {
					url: "/business/label",
					method: "post"
				};

				console.log("上传新建信息");

				uni.showLoading({
					title: '加载中'
				});

				request.httpTokenRequest(opts, this.label_data).then(res => {
					uni.hideLoading();
					if (res.code == 200) {
						console.log(res);
					} else {
						console.log("请求失败");
					}
				});

				// #ifdef APP-NVUE
				const eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
				// #endif
				// #ifndef APP-NVUE
				const eventChannel = this.getOpenerEventChannel();
				// #endif
				//回复事件acceptDataFromOpenedPage
				eventChannel.emit('acceptDataFromOpenedPage', this.formData);
				
				console.log("you clicked save, then we will back..");
				
				uni.navigateBack({
					delta:1
				})

			},
			// 扫码
			scan() {
				console.log("you clicked scan..");
			},

		}

	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}

	.tagCode {
		margin: auto;
	}
</style>
